﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using DAO;
using BUS;
public partial class Admin_Pages_WRFUNCPAYManager : BasePage
{
    public static bool Insert;
    BUS_WRFUNCPAY busWRFUNCPAY = new BUS_WRFUNCPAY();
    BUS_WRUSER busWruser = new BUS_WRUSER();
    private void Show(string Mess)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + Mess + "')", true);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            pnUpdate.Visible = false;
            pnShow.Visible = true;
            BindGrid();
        }
    }

    private void BindGrid()
    {
        grvWRFUNCPAY.DataSource = busWRFUNCPAY.Get_FuncPay_List();
        //grvWRFUNCPAY.DataSource = busWRFUNCPAY._GetList().Select(p=>new
        //{
        //    p.ID,
        //    p.FuncPayName,
        //    p.FuncPayVal,
        //    p.Description,
        //    p.Active,
        //    CreateDate = Common.Common.ConvertDateTime(p.CreateDate.ToString()) == "" ? "" : Convert.ToDateTime(p.CreateDate).ToShortDateString(),
        //    UpdateDate = Common.Common.ConvertDateTime(p.UpdateDate.ToString()) == "" ? "" : Convert.ToDateTime(p.UpdateDate).ToShortDateString(),
        //    CreateBy = p.IDCreateBy == (int?)null ? "" : busWruser._GetObject(Convert.ToInt32(p.IDCreateBy)).Lastname + " " + busWruser._GetObject(Convert.ToInt32(p.IDCreateBy)).Firstname,
        //    UpdateBy = p.IDUpdateBy == (int?)null ? "" : busWruser._GetObject(Convert.ToInt32(p.IDUpdateBy)).Lastname + " " + busWruser._GetObject(Convert.ToInt32(p.IDUpdateBy)).Firstname
        //}).ToList();
        grvWRFUNCPAY.DataBind();
    }

    public void BindDrop()
    {

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = true;
        pnShow.Visible = false;
        Insert = true;
        txtFuncPayName.Text = "";
        txtFuncPayVal.Text = "";
        txtDescription.Text = "";


    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (grvWRFUNCPAY.Items.Count > 0)
        {
            for (int i = 0; i < grvWRFUNCPAY.Items.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)grvWRFUNCPAY.Items[i].FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    string Id = grvWRFUNCPAY.Items[i].Cells[1].Text;
                    try
                    {
                        busWRFUNCPAY._Delete(Convert.ToInt32(Id));
                    }
                    catch
                    {
                    }
                }
            }
        }
        BindGrid();
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        if (txtFuncPayName.Text.Trim().Equals(""))
        {
            Show("FuncPayName is required!");
            txtFuncPayName.Focus();
            return;
        }
        if (!Common.Common.IsNumeric(txtFuncPayVal.Text.Trim()))
        {
            Show("FuncPayVal is a number!");
            txtFuncPayVal.Focus();
            return;
        }
        WRFUNCPAY obj = new WRFUNCPAY();
        obj.FuncPayName = txtFuncPayName.Text;
        obj.FuncPayVal = txtFuncPayVal.Text;
        obj.Description = txtDescription.Text;
        obj.Active = dropActive.SelectedValue;
        obj.UpdateDate = DateTime.Now;
        obj.IDUpdateBy = Convert.ToInt32(Session["userid"].ToString());
        if (Insert)
        {
            obj.IDCreateBy = Convert.ToInt32(Session["userid"].ToString());
            obj.CreateDate = DateTime.Now;
            busWRFUNCPAY._Add(obj);
        }
        else
        {
            obj.ID = Convert.ToInt32(txtID.Text);
            busWRFUNCPAY._Update(obj);
        }
        BindGrid();
        pnUpdate.Visible = false;
        pnShow.Visible = true;
        Insert = false;

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = false;
        pnShow.Visible = true;

    }

    protected void grvWRFUNCPAY_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        string Id = e.CommandArgument.ToString();
        if (e.CommandName == "Edit")
        {
            Insert = false;
            WRFUNCPAY _WRFUNCPAY = busWRFUNCPAY._GetObject(Convert.ToInt32(Id));
            txtID.Text = Id;
            txtFuncPayName.Text = _WRFUNCPAY.FuncPayName;
            txtFuncPayVal.Text = _WRFUNCPAY.FuncPayVal;
            txtDescription.Text = _WRFUNCPAY.Description;
            dropActive.SelectedValue = _WRFUNCPAY.Active.ToString();

            pnUpdate.Visible = true;
            pnShow.Visible = false;
        }
        else if (e.CommandName == "Delete")
        {
            busWRFUNCPAY._Delete(Convert.ToInt32(Id));
            BindGrid();
        }

    }

    protected void grvWRFUNCPAY_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        grvWRFUNCPAY.CurrentPageIndex = e.NewPageIndex;
        BindGrid();
    }
}
